﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;
using Knlght.CH.Domain.IRepository;
using Knlght.Repository;
using Knlght.ObjectFactory.Core;
using Knlght.Service;
using StackExchange.Profiling;
using Knlght.Model.Sys;
using Knlght.Common.Security;

namespace Knlght.Web
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            //绑定对象工厂
            BindObjects();
            // 在应用程序启动时运行的代码
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            MiniProfiler.Settings.Results_Authorize = Request =>
            {
                var name = Request.Cookies["cxh"] == null ? "" : Request.Cookies["cxh"].Value;                            
                return name.Equals("ADMIN");
            };

            StackExchange.Profiling.EntityFramework6.MiniProfilerEF6.Initialize();


          
           
        
        }
        //对象绑定
        private void BindObjects()
        {
            ObjectFactory.ObjectFactory.BindSingle<ISettingApi>(() => new DbSettingApi());
            ObjectFactory.ObjectFactory.Bind<ICache>(() => new Cache());
            //Repositories                     
            ObjectFactory.ObjectFactory.Bind<IAdministratorsRepository>(() => new AdministratorsRepository());
            ObjectFactory.ObjectFactory.Bind<IRoleInfoRepository>(() => new RoleInfoRepository());
            ObjectFactory.ObjectFactory.Bind<ILogInfoRepository>(() => new LogInfoRepository());
            ObjectFactory.ObjectFactory.Bind<INewsTypeRepository>(() => new NewsTypeRepository());
            ObjectFactory.ObjectFactory.Bind<INewsInfoRepository>(() => new NewsInfoRepository());
            ObjectFactory.ObjectFactory.Bind<ILinkInfoRepository>(() => new LinkInfoRepository());
            ObjectFactory.ObjectFactory.Bind<ISettingRepository>(() => new SettingRepository());

        }
        protected void Application_BeginRequest()
        {
            MiniProfiler.Start();
        }
        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }
    }
}